// mypage/checkMech/checkMech.js
var app=getApp();
var my=require("../../api/my");



Page({
  /*页面的初始数据*/
  data: {
    type: '',
    mechs: [],
    cur_index: 0,
    token: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    that.setData({cur_index:options.index,token:wx.getStorageSync('token')})
    wx.showLoading({title:"加载中"})
    //
    that.get_org_audi_list(options.index)
  },
  // 选择
  check_mech:function(e){
    var that=this,mechs=that.data.mechs;
    var index=e.currentTarget.dataset.index;
    mechs.forEach((item,i)=>{
      mechs[i].checked=false;
      mechs[index].checked=true;
    })
    that.setData({mechs:mechs,cur_index:index})
  },
  confirm_mech:function(){
    var that=this,mechs=that.data.mechs,cur_index=that.data.cur_index;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({mech_index:cur_index,mech_id: mechs[cur_index].businessId,businessName:mechs[cur_index].businessName,update:true})
    wx.navigateBack({
      delta: 1
    })
  },
  // 获取分校列表
  get_org_audi_list:function(index){
    var that=this,token=that.data.token,phone=wx.getStorageSync('userinfo').phone;
    var params={
      "phone": phone
    }
    my.get_org_audi_list(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        for(var i=0;i<list.length;i++){
          list[i].checked=false;
          list[index].checked=true;
        }
        that.setData({mechs: list})
      }
    })
  },
})